package com.yyj.web.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.yyj.common.BaseResponse;
import com.yyj.constant.WebConstant;
import com.yyj.enums.PaymentStatusEnum;
import com.yyj.enums.PaymentTypeEnum;
import com.yyj.pojo.user.SessionInfo;
import com.yyj.service.PaymentService;
import com.yyj.service.WaterBillService;
import com.yyj.service.WeixinPayService;
import com.yyj.util.DateUtil;
import com.yyj.util.RspUtils;
import com.yyj.web.protocol.PageRequest;
import com.yyj.web.protocol.req.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Api(value = "生活缴费相关接口" , description="生活缴费相关接口")
@RestController
public class PaymentContoller extends BaseController {

    @Autowired
    PaymentService paymentService;

    @Autowired
    WeixinPayService weixinPayService;

    @Autowired
    WaterBillService waterBillService;

    @ApiOperation(value = "查询业主当月费用")
    @RequestMapping(value = "/auth/payments/current/{type}", method = RequestMethod.GET)
    public BaseResponse queryPayment(@PathVariable String type
    ){
        try{
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            PaymentQueryReq paymentQueryReq = new PaymentQueryReq();
            paymentQueryReq.setRoomNum(sessionInfo.getRoomNum());
            paymentQueryReq.setFeeDate(DateUtil.getNowShortDate().substring(0 , 6));
            paymentQueryReq.setType(type);
            return RspUtils.success(paymentService.findPaymentRecord(paymentQueryReq));
        }catch(Exception e){
            logger.error("查询业主当月费用异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "查询业主历史费用列表")
    @RequestMapping(value = "/auth/payments/{type}", method = RequestMethod.GET)
    @ApiParam(name = "费用类型"  , example = "SANITARY_FEE")
    public BaseResponse queryPaymentList(@PathVariable String type , PageRequest pageRequest
    ){
        try{
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            PaymentQueryReq paymentQueryReq = new PaymentQueryReq();
            paymentQueryReq.setCurrentPage(pageRequest.getCurrentPage());
            paymentQueryReq.setPageSize(pageRequest.getPageSize());
            paymentQueryReq.setRoomNum(sessionInfo.getRoomNum());
            paymentQueryReq.setType(type);
            return RspUtils.success(paymentService.findPaymentList(paymentQueryReq));
        }catch(Exception e){
            logger.error("查询业主历史费用列表异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "查询业主历史费用详情")
    @RequestMapping(value = "/auth/payments/detail/{id}", method = RequestMethod.GET)
    public BaseResponse queryPaymentList(@PathVariable Long id
    ){
        try{
            return RspUtils.success(paymentService.findPaymentDetail(id));
        }catch(Exception e){
            logger.error("查询业主历史费用详情异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "业主生活缴费")
    @RequestMapping(value = "/auth/payments/pay", method = RequestMethod.POST)
    public BaseResponse payBill(@RequestBody @Validated PaymentPayReq paymentPayReq , BindingResult result
    ){
        try{
            logger.info("业主生活缴费入参："+JSON.toJSONString(paymentPayReq));
            checkRequestParams(result);
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            return RspUtils.success(paymentService.payBill(paymentPayReq , sessionInfo));
        }catch(Exception e){
            logger.error("业主生活缴费异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    /**
     * 微信支付回调
     * @param request
     * @return
     */
    @ApiIgnore
    @RequestMapping(value = "/payments/wxPayNotify" , method = RequestMethod.POST)
    public Object wxPayNotify(HttpServletRequest request

    ){
        try{
            String backStr = weixinPayService.payCallback(request);
            return backStr;
        }catch(Exception e){
            logger.error("wxPayNotify error:{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "费用类型说明")
    @RequestMapping(value = "/payments/type", method = RequestMethod.GET)
    public BaseResponse paymentType(
    ){
        try{
            HashMap<String , String> typeMap = Maps.newHashMap();
            for(PaymentTypeEnum paymentTypeEnum : PaymentTypeEnum.values()){
                typeMap.put(paymentTypeEnum.getCode() , paymentTypeEnum.getDesc());
            }
            return RspUtils.success(typeMap);
        }catch(Exception e){
            logger.error("业主生活缴费异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "费用类型说明")
    @RequestMapping(value = "/payments/status", method = RequestMethod.GET)
    public BaseResponse paymentStatus(
    ){
        try{
            HashMap<String , String> typeMap = Maps.newHashMap();
            for(PaymentStatusEnum paymentStatusEnum : PaymentStatusEnum.values()){
                typeMap.put(paymentStatusEnum.getCode() , paymentStatusEnum.getDesc());
            }
            return RspUtils.success(typeMap);
        }catch(Exception e){
            logger.error("业主生活缴费异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "业主查看水费缴费记录详情")
    @RequestMapping(value = "/auth/payments/waterBills/detail/{waterBillId}", method = RequestMethod.GET)
    public BaseResponse queryWaterBillDetail2(@PathVariable long waterBillId
    ){
        try{
            return RspUtils.success(waterBillService.queryWaterBillInfo(waterBillId));
        }catch(Exception e){
            logger.error("业主查看水费信息异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "查看收费说明")
    @RequestMapping(value = "/auth/payments/info", method = RequestMethod.GET)
    public BaseResponse paymentInfo() {
        try {
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            return RspUtils.success(paymentService.getPaymentInfo(sessionInfo));
        } catch (Exception e) {
            return RspUtils.error(e.getMessage());
        }
    }

}
